package so.shanku.essential.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.AttachPicAdapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.Utils;
import striveen.util.used.view.view.HorizontalListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 15/10/24.
 * 填写退货物流信息
 */
public class DoReturnProductActivity extends BaseUIActivity {
	/**
	 * 问题描述
	 */
	@ViewInject(id = R.id.ques_discri_tv)
	private TextView ques_discri_tv;
	/**
	 * 商品图片附件
	 */
	@ViewInject(id = R.id.attach_pic_hlv)
	private HorizontalListView attach_pic_hlv;

	@ViewInject(id = R.id.btn_confirm, click = "btn_confirm_click")
	private Button confirm_bt;

	/**
	 * 收货人信息
	 */
	@ViewInject(id = R.id.receiver_name_tv)
	private TextView receiver_name_tv;
	@ViewInject(id = R.id.receiver_tel_tv, click = "receiver_tel_tv_click")
	private TextView receiver_tel_tv;
	@ViewInject(id = R.id.receiver_address_tv)
	private TextView receiver_address_tv;

	/**
	 * 快递方式
	 */
	@ViewInject(id = R.id.express_ed)
	private EditText express_ed;

	/**
	 * 快递单号
	 */
	@ViewInject(id = R.id.express_num_ed)
	private EditText express_num_ed;

	/**
	 * 退款单号
	 */
	private String order_num_str;
	private String vendorId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.do_return_layout);
		setCenter_title(R.string.do_goods_return);
		order_num_str = getIntent().getStringExtra(ExtraKeys.Key_Msg1);
		vendorId = getIntent().getStringExtra(ExtraKeys.Key_Msg2);
		//getExpressInfos();
		confirm_bt.setClickable(false);
		getReturnAddress();
	}

	/**
	 * 获取退货地址
	 */
	private void getReturnAddress() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("GoodsReturnNum", order_num_str);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_GoodsReturnDetail);
		queue.setCallBack(callBack);
		queue.setParamsNoJson(params);
		queue.setWhat(GetDataConfing.What_GoodsReturnDetail);
		getDataUtil.getData(queue);
	}


	/**
	 * 获取快递信息
	 */
	private void getExpressInfos() {
//        loadingToast.show();
//        Map<String, Object> params = new HashMap<String, Object>();
//
//
//        GetDataQueue queue = new GetDataQueue();
//        queue.setActionName(GetDataConfing.Action_GetDiscoverDetails);
//        queue.setParamsNoJson(params);
//        queue.setCallBack(callBack);
//        queue.setWhat(GetDataConfing.What_GetDiscoverDetails);
//        getDataUtil.getData(queue);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.express_data));
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	}

	/**
	 * 确认退货  {GoodsReturnNum:"H000056",LogisticsNum:"123456",ExpressCompany:"xxxxx",UserName:"xia"}
	 *
	 * @param view
	 */
	public void btn_confirm_click(View view) {
		String express_way = express_ed.getText().toString();
		if (TextUtils.isEmpty(express_way)) {
			express_ed.requestFocus();
			toast.showToast(R.string.input_express_way);
			return;
		}
		String express_num = express_num_ed.getText().toString();
		if (TextUtils.isEmpty(express_num)) {
			express_num_ed.requestFocus();
			toast.showToast(R.string.input_express_num);
			return;
		}

		loadingToast.show();
		loadingToast.update(R.string.fill_return_uploading);
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("GoodsReturnNum", order_num_str);
		params.put("LogisticsNum",express_num );
		params.put("ExpressCompany", express_way);
		params.put("UserName", Utils.getUserAccount(mContext));
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_Fill_ExpressInfo_ReturnGoods);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		queue.setWhat(GetDataConfing.What_Fill_ExpressInfo_ReturnGoods);
		getDataUtil.getData(queue);
	}


	/**
	 * 获取服务器数据的返回信息
	 */
	private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.What_Fill_ExpressInfo_ReturnGoods) {
						//setOrderDetail(data);
						toast.showToast(R.string.fill_return_reason_success);
						finish();
					} else if (entity.what == GetDataConfing.What_GoodsReturnDetail) {
						JsonMap jsonMap = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info).get(0);
						JsonMap returnInfoJsonMap=jsonMap.getJsonMap("ReturnInfo") ;
						List<JsonMap<String,Object>> jsonMapList=jsonMap.getList_JsonMap("GoodsReturnOrderDetailList");
						JsonMap goodsReturnOrderDetailList=jsonMapList.get(0) ;
						setAddressData(returnInfoJsonMap);
						setQuestionInfo(goodsReturnOrderDetailList);
						confirm_bt.setClickable(true);
					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};

	/**
	 * 设置反馈信息
	 * @param goodsReturnOrderDetailList
	 */
	private void setQuestionInfo(JsonMap goodsReturnOrderDetailList) {
		 ques_discri_tv.setText(goodsReturnOrderDetailList.getStringNoNull("ReturnReason"));
		ArrayList<JsonMap<String,Object>>attachPics= (ArrayList<JsonMap<String, Object>>) goodsReturnOrderDetailList.getList_JsonMap("GoosImgslList");
		if(attachPics.size()>0) {
			attach_pic_hlv.setAdapter(new AttachPicAdapter(mContext, attachPics));
			attach_pic_hlv.setVisibility(View.VISIBLE);
		}else {
			attach_pic_hlv.setVisibility(View.GONE);
		}
	}

	String phoneStr;

	/**
	 * 设置退货商家地址信息
	 * @param jsonMap1
	 */
	private void setAddressData(JsonMap jsonMap1) {
		JsonMap returnInfoJsonMap=jsonMap1;
		receiver_address_tv.setText(returnInfoJsonMap.getStringNoNull("DetailAddress"));
		receiver_name_tv.setText(returnInfoJsonMap.getStringNoNull("LinkMan"));
		phoneStr = returnInfoJsonMap.getStringNoNull("Phone");
		if (TextUtils.isEmpty(phoneStr)) {
			phoneStr = returnInfoJsonMap.getStringNoNull("Tel");
			receiver_tel_tv.setText(phoneStr);
		} else {
			receiver_tel_tv.setText(phoneStr);
		}
	}

	/**
	 * 电话点击
	 *
	 * @param view
	 */
	public void receiver_tel_tv_click(View view) {
		startTelPhone(phoneStr);
	}

}